#include "stdafx.h"
#include "SegmentManager.h"
#include "Selection.h"
#include "Text.h"
#include "LetterCode.h"

vector<Segment> SegmentManager::AnalyzeString(int row)
{
	using LetterCode::IsTab;

	vector<Segment> segments;
	wstring str = text->GetString(row);

	int start = 0;
	int i = 0;
	bool isPrevInSelection = sel->IsPtInSelection(row, 0);
	while(i < int(str.size()))
	{
		Segment seg;
		seg.isTab = false;
		seg.stat = NONE;
		for(i = start; i < int(str.size()); i++)
		{
			bool isBreak = false;
			if(sel->IsPtInSelection(row, i))
			{
				if(!isPrevInSelection)
				{
					isBreak = true;
				}
				isPrevInSelection = true;
			}
			else
			{
				if(isPrevInSelection) 
				{ 
					isBreak = true;
					seg.stat = SEL;
				}
				isPrevInSelection = false;
			}

			if(IsTab(str[i]))
			{
				isBreak = true;
				seg.isTab = true;
				seg.stat = (sel->IsPtInSelection(row, i)? SEL : NONE);
				i++;
			}
			if(isBreak) break;
		}
		if(i == int(str.size())) seg.stat = isPrevInSelection? SEL: NONE;

		seg.start = start;
		seg.end = i;
		segments.push_back(seg);

		start = i;
	}
	return segments;
}
/*
vector<Segment> SegmentManager::AnalyzeString2(int row)
{

}//*/

void SegmentManager::Init(Text* text, Selection* sel)
{
	this->text = text;
	this->sel = sel;
}

SegmentManager::SegmentManager(void)
{
}

SegmentManager::~SegmentManager(void)
{
}
